nonLinearRegressionDialog <- function(){
	initializeDialog(title=gettext("Non linear regression", domain="R-RcmdrPlugin.TeachingExtras"))
	variablesFrame <- tkframe(top)
	.numeric <- Numeric()
	xBox <- variableListBox(variablesFrame, .numeric, title=gettext("Explanatory variables (pick one)", domain="R-RcmdrPlugin.TeachingExtras"))
	yBox <- variableListBox(variablesFrame, .numeric, title=gettextRcmdr("Response variable (pick one)"))
	UpdateModelNumber()
	modelName <- tclVar(paste("RegModel.", getRcmdr("modelNumber"), sep=""))
	modelNameFrame <- tkframe(top)
	model <- ttkentry(modelNameFrame, width="20", textvariable=modelName)
	subsetBox()
	onOK <- function(){
		x <- getSelection(xBox)
		y <- getSelection(yBox)
		closeDialog()
		if (0 == length(y)) {
			UpdateModelNumber(-1)
			errorCondition(recall=nonLinearRegressionDialog, message=gettextRcmdr("You must select a response variable."))
			return()
		}
		if (0 == length(x)) {
			UpdateModelNumber(-1)
			errorCondition(recall=nonLinearRegressionDialog, message=gettextRcmdr("No explanatory variables selected."))
			return()
		}
		if (is.element(y, x)) {
			UpdateModelNumber(-1)
			errorCondition(recall=nonLinearRegressionDialog, message=gettextRcmdr("Response and explanatory variables must be different."))
			return()
		}
		subset <- tclvalue(subsetVariable)
		if (trim.blanks(subset) == gettextRcmdr("<all valid cases>") || trim.blanks(subset) == ""){
			subset <- ""
			putRcmdr("modelWithSubset", FALSE)
		}
		else{
			subset <- paste(", subset=", subset, sep="")
			putRcmdr("modelWithSubset", TRUE)
		}
		modelValue <- trim.blanks(tclvalue(modelName))
		if (!is.valid.name(modelValue)){
			UpdateModelNumber(-1)
			errorCondition(recall=nonLinearRegressionDialog, message=sprintf(gettextRcmdr('"%s" is not a valid name.'), modelValue))
			return()
		}
		if (is.element(modelValue, listLinearModels())) {
			if ("no" == tclvalue(checkReplace(modelValue, type=gettextRcmdr("Model")))){
				UpdateModelNumber(-1)
				nonLinearRegressionModel()
				return()
			}
		}
		regModel <- tclvalue(regModelVariable)
		if (regModel == "cuadratic")
			command <- paste("lm(", y, "~", x,	"+I(", x, "^2), data=", ActiveDataSet(), subset, ") #Cuadratic regression y=a+b*x+c*x^2", sep="")
		else if (regModel == "cubic")
			command <- paste("lm(", y, "~", x,	"+I(", x, "^2)+I(", x, "^3), data=", ActiveDataSet(), subset, ") #Cubic regression y=a+b*x+c*x^2+d*x^3", sep="")
		else if (regModel == "potential")
			command <- paste("lm(log(", y, ")~log(", x,	"), data=", ActiveDataSet(), subset, ") #Potencial regression log(y)=log(a)+b*log(x)", sep="")
		else if (regModel == "exponential")
			command <- paste("lm(log(", y, ")~", x,	", data=", ActiveDataSet(), subset, ") #Exponential regression log(y)=a+bx", sep="")
		else if (regModel == "logarithmic")
			command <- paste("lm(", y, "~log(", x,	"), data=", ActiveDataSet(), subset, ") #Logarithmic regression y=a+b*log(x)", sep="")
		else if (regModel == "inverse")
			command <- paste("lm(", y, "~I(1/", x,	"), data=", ActiveDataSet(), subset, ") #Inverse regression y=a+b*1/x", sep="")
		else if (regModel == "scurve")
			command <- paste("lm(log(", y, ")~I(1/", x,	"), data=", ActiveDataSet(), subset, ") #S curve regression log(y)=a+b*1/x", sep="")
		else {
			UpdateModelNumber(-1)
			errorCondition(recall=nonLinearRegressionDialog, message=gettext('You must select a regression model.', domain="R-RcmdrPlugin.TeachingExtras"))
			return()
		}
			
		logger(paste(modelValue, " <- ", command, sep=""))
		assign(modelValue, justDoIt(command), envir=.GlobalEnv)
		doItAndPrint(paste("summary(", modelValue, ")", sep=""))
		activeModel(modelValue)
		tkfocus(CommanderWindow())
	}
	OKCancelHelp(helpSubject="lm", model=TRUE)
	tkgrid(labelRcmdr(modelNameFrame, text=gettextRcmdr("Enter name for model:")), model, sticky="w")
	tkgrid(modelNameFrame, sticky="w")
	tkgrid(getFrame(yBox), labelRcmdr(variablesFrame, text="    "), getFrame(xBox), sticky="nw")
	tkgrid(variablesFrame, sticky="w")
	radioButtons(top, name="regModel", buttons = c("cuadratic", "cubic", "potential", "exponential", "logarithmic", "inverse", "scurve"), labels = gettext(c("Cuadratic", "Cubic", "Potential", "Exponential", "Logarithmic", "Inverse", "S curve"), domain="R-RcmdrPlugin.TeachingExtras"), title = gettext("Models:", domain="R-RcmdrPlugin.TeachingExtras"))
	tkgrid(regModelFrame, sticky = "w")
	tkgrid(subsetFrame, sticky="w")
	tkgrid(buttonsFrame, stick="w")
	tkgrid.configure(helpButton, sticky="e")
	dialogSuffix(rows=5, columns=1)
}
